package com.example.fengshoutong.adapter;

import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.fengshoutong.R;
import com.example.fengshoutong.bean.Good;
import com.example.fengshoutong.ui.CarFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车适配器
 */
public class CarAdapter extends RecyclerView.Adapter<CarAdapter.ViewHolder> {
    private List<Good> mDataList = new ArrayList<>();
    private boolean allSelect;
    private CarFragment.setOnItemCheck setOnItemCheck;

    public CarAdapter(List<Good> dataList) {
        this.mDataList = dataList;
    }

    /**
     * 全选
     */
    public void setAllSelect() {
        allSelect = true;
        notifyDataSetChanged();
    }

    public void setSetOnItemCheck(CarFragment.setOnItemCheck setOnItemCheck) {
        this.setOnItemCheck = setOnItemCheck;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_car, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Good data = mDataList.get(position);
        holder.image_car.setImageURI(Uri.parse(data.getImageUri()));
        holder.text_title.setText(data.getLabel());
        holder.text_price.setText(data.getPrice());
        holder.radio_car.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (setOnItemCheck == null) {
                    return;
                }
                if (isChecked) {
                    setOnItemCheck.onCheckPosition(position);
                } else {
                    setOnItemCheck.onDisCheckPosition(position);
                }
            }
        });
        if (allSelect) {
            holder.radio_car.setChecked(true);
        }
    }

    @Override
    public int getItemCount() {
        if (mDataList != null) {
            return mDataList.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        RadioButton radio_car;
        ImageView image_car;
        TextView text_title;
        TextView text_price;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            radio_car = itemView.findViewById(R.id.radio_car);
            image_car = itemView.findViewById(R.id.image_car);
            text_title = itemView.findViewById(R.id.text_title);
            text_price = itemView.findViewById(R.id.text_price);
        }
    }
}
